//
//  CLHeadView.m
//  02-QQ好友数据加载
//
//  Created by lin on 15/8/19.
//  Copyright (c) 2015年 itxdl. All rights reserved.
//

#import "CLHeadView.h"

#import "QQFriendGroup.h"

@interface CLHeadView ()

@property (nonatomic, weak) UIButton *backGroupBtn;

@property (nonatomic, weak) UILabel *countLab;

@end

@implementation CLHeadView

+ (instancetype)headViewWith:(UITableView *)tableView {
    static NSString *headIdentfiy = @"headView";
    CLHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headIdentfiy];
    if (headView == nil) {
        headView = [[CLHeadView alloc] initWithReuseIdentifier:headIdentfiy];
    }
    return headView;
}

// 在初始化方法中不可以设置frame  只能添加控件
// 创建自定义的UITableViewHeadFootView
// 设置背景颜色 对其 字体等一次性操作方法初始化方法中

// clickToBounds  === layer.masksToBounds 设置等价
// 如果设置为YES超出部分将会被剪切掉
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        
        [button setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        
        [button setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        
        [button setContentEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
        
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
        
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        button.imageView.contentMode =  UIViewContentModeCenter;
        
        button.imageView.clipsToBounds = NO;
    
        [self.contentView addSubview:button];
        self.backGroupBtn = button;
        
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentRight;
//        label.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:label];
        self.countLab = label;
    }
    return self;
}

// 1 这个方法时在设置frame时调用 可以覆盖父类里面的方法 对里面的子控件进行重新布局
// 2 必须调用父类的layouSubViews 方法
- (void)layoutSubviews {
#warning 必须调用父类的layoutSubViews方法
    [super layoutSubviews];
    CGFloat buttonX = 0;
    CGFloat buttonY = 0;
    CGFloat buttonW = self.contentView.bounds.size.width;
    CGFloat buttonH = self.contentView.bounds.size.height;
    self.backGroupBtn.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
    
    CGFloat rightMargin = 10;
    CGFloat labelW = 100;
    CGFloat labelH = self.contentView.bounds.size.height;
    CGFloat labelY = 0;
    CGFloat labelX = self.contentView.bounds.size.width - rightMargin - labelW;
    self.countLab.frame = CGRectMake(labelX, labelY, labelW, labelH);
}


- (void)setFriendGroup:(QQFriendGroup *)friendGroup {
    _friendGroup = friendGroup;
    [self.backGroupBtn setTitle:friendGroup.name forState:UIControlStateNormal];
    
    self.countLab.text = [NSString stringWithFormat:@"%@/%ld",friendGroup.online, friendGroup.groupFriend.count];
}

- (void)btnClick:(UIButton *)btn {
    // 关闭和打开
    self.friendGroup.open = !self.friendGroup.isOpen;
    

    if ([self.delegate respondsToSelector:@selector(headViewDidClick:)]) {
        [self.delegate headViewDidClick:self];
    }
}

//  将控件addSubView时调用的方法
- (void)didMoveToSuperview {
    // 旋转箭头
    if (self.friendGroup.isOpen) {
        self.backGroupBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }else {
        self.backGroupBtn.imageView.transform = CGAffineTransformMakeRotation(0);
    }
}


@end
